<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Hash;

use App\Http\Controllers\common\MyAgentController;

use App\Http\Controllers\Controller;

use DB;

class StaffController extends Controller{

    public function getIndex(Request  $request){
        $uri['uri'] = base64_encode($_SERVER['REQUEST_URI']);
        $get['linkman'] = $request->input('linkman','');
        $agentId = $request->cookie('agentId');
        $get['ag'] = $request->input('ag',$agentId);
        //查询父级代理商path
        $path = DB::table('sa_agent') -> select('path') -> where(['id'=>$agentId]) -> first();
        $len = substr_count($path['path'],',');
        $agent = MyAgentController::allAgent($agentId);
        foreach ($agent as $k=>$v){
            $agent[$k]['agent_name'] = str_repeat('--',substr_count($v['path'],',')-$len)."&nbsp;&nbsp;".$v['agent_name'];
        }
        //查询admin表
        $data = DB::table('sa_admin') -> where('agent_id','=',$get['ag']) -> where('linkman','like','%'.$get['linkman'].'%') -> paginate(10);
        $info = $data->all();
        return view('admin.staff.index',['agent'=>$agent,'data'=>$data,'info'=>$info,'request'=>$request->all(),'get'=>$get,'uri'=>$uri]);
    }

    //添加员工账号页面
    public function getCreate(){
        return view('admin.staff.create');
    }

    //添加员工账号处理
    public function postCreate(Request $request){
        $data = $request -> except('_token','password1');
        $data['password'] = Hash::make($data['password']);
        $data['auth'] = 2;
        $data['agent_id'] = $request -> cookie('agentId');
        //插入admin表
        $insertId = DB::table('sa_admin') -> insertGetId($data);
        if($insertId){
            return redirect('/admin/staff/index') -> with('success','添加成功');
        }else{
            return back() -> withInput() -> with('error','添加失败');
        }
    }

    //员工账号编辑页面
    public function getEdit(Request $request){
        $get['uri'] = $request -> input('uri');
        $id = $request -> input('id');
        //查询sa_admin表
        $data = DB::table('sa_admin') -> where('id','=',$id) -> first();
        //dd($data);
        return view('admin.staff.edit',['data'=>$data,'get'=>$get]);
    }

    //员工账号编辑处理页面
    public function postEdit(Request $request){
        $id = $request -> input('staff_id');
        $uri = base64_decode($request -> input('uri'));
        $account = $request -> input('account');
        $linkman = $request -> input('linkman');
        $phone = $request -> input('phone');
        //修改sa_admin表
        $row = DB::table('sa_admin') -> where('id','=',$id) ->update(compact('account','linkman','phone'));
        if($row){
            return redirect($uri) -> with('success','编辑成功');
        }else{
            return back() -> withInput() -> with('error','编辑失败');
        }
    }

    //修改密码编辑处理页面
    public function postEdit1(Request $request){
        $id = $request -> input('staff_id');
        $password = $request -> input('password');
        $password1 = $request -> input('password1');
        $data['password'] = Hash::make($password1);
        //判断该账号密码是否输入正确
        $res = DB::table('sa_admin') -> select('password') -> where('id','=',$id) -> first();
        if (Hash::check($password, $res['password'])) {
            //密码正确可修改密码
            $row = DB::table('sa_admin') -> where('id','=',$id) -> update($data);
            if($row){
                if($request -> cookie('agentId') == $id){
                    return redirect('/admin/login/index') -> with('success','密码修改成功,请重新登录');
                }else{
                    return redirect('/admin/staff/index') -> with('success','密码修改成功');
                }
            }else{
                return back() -> withInput() -> with('error','密码修改失败');
            }
        }else{
            return back() -> withInput() -> with('error','请输入正确的原密码');
        }
        /*if($row){
            return redirect('/admin/staff/index') -> with('success','编辑成功');
        }else{
            return back() -> withInput() -> with('error','编辑失败');
        }*/
    }

    //编辑权限页面
    public function getAuth(Request $request){
        $id = $request -> input('id');
        //获取登录代理商id或搜索的代理商id
        $agentId = $request->cookie('agentId');
        $get['ag'] = $request->input('ag',$agentId);
        //查询代理商下的所有区域
        $data = DB::table('sa_area') -> where('a_agent_id','=',$get['ag']) -> get();
        //查询账号表中id号为$id的管理范围
        $res = DB::table('sa_admin') -> where('id','=',$id) -> select('scope') -> first();
        $scope = explode(',',$res['scope']);
        return view('admin.staff.auth',['id'=>$id,'data'=>$data,'scope'=>$scope]);
    }

    //编辑权限处理页面
    public function postAuth(Request $request){
        $accountId = $request -> input('accountId');
        $data = $request -> input('data');
        $scope = implode(',',$data);
        //修改登录账号表
        $res = DB::table('sa_admin') -> where('id','=',$accountId) -> update(['scope'=>$scope]);
        if($res){
            $info['status'] = '1';
            $info['msg'] = '权限分配成功';
        }else{
            $info['status'] = '-1';
            $info['msg'] = '权限分配失败';
        }
        echo json_encode($info);
    }
    //验证账号是否已存在
    public function postTest(Request $request){
        $account = $request -> input('account');

        //查询申请代理商表
        $row = DB::table('sa_admin') -> where(['account'=>$account]) -> first();
        if($row){
            $data['status'] = '-1';
            $data['msg'] = '账号已存在';
        }else{
            $data['status'] = '1';
        }
        echo json_encode($data);
    }

    //账号启用禁用
    public function postStatus(Request $request){
        $info = $request -> except('_token');
        if($info['status'] == 1){
            $row = DB::table('sa_admin') -> where('id','=',$info['id']) -> update(['status'=>0]);
            if($row){
                $data['status'] = 0;
            }else{
                $data['status'] = 1;
            }
        }else{
            $row = DB::table('sa_admin') -> where('id','=',$info['id']) -> update(['status'=>1]);
            if($row){
                $data['status'] = 1;
            }else{
                $data['status'] = 0;
            }
        }
        $data['id'] = $info['id'];
        return json_encode($data);
    }
}